package com.hb;


import com.github.pagehelper.PageHelper;
import com.hb.mapper.BookMapper;
import com.hb.pojo.Book;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

// 参考 https://blog.csdn.net/nmsoftklb/article/details/146100297
// 方式一：直接使用 SqlSession 的 API (不推荐)
public class MybatisPageHelperTest03 {
    public static void main(String[] args) {
        try {
            //加载配置全局文件
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            //创建SqlSessionFactory工厂
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = builder.build(inputStream);
            //使用工厂生产SqlSession对象
            SqlSession session = sqlSessionFactory.openSession();
            //从SQLSession中获取接口实现类对象
            BookMapper mapper = session.getMapper(BookMapper.class);
            int pageNum = 2;
            int pageSize = 1;
            PageHelper.startPage(pageNum,pageSize);
            //执行-查全部
            List<Book> books = mapper.findAll();
            books.forEach(item -> {
                System.out.println("item = " + item);
            });
            System.out.println(books);

            //释放资源
            session.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}